package com.xywy.find;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.viewpagerindicator.TabPageIndicator;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.widget.TitleBar2;
import com.xywy.find.bean.Jkgc;

public class NewJkgcActivity extends BaseActivity {

	public static final String[] TITLE = new String[] { "母婴", "美容", "保健", "饮食",
			"减肥" };

	public static final String[] TITLE_KEY = new String[] { "xywy_my",
			"xywy_mr", "xywy_bj", "xywy_ys", "xywy_jf" };

	private ViewPager pager;
	private List<Jkgc> jkgcList;
	private String target = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.find_jkgc_index_new);
		this.preInit();
		this.initView();
		this.tabPage();
		this.loadData();
	}

	@Override
	public void preInit() {

	}

	@Override
	public void loadData() {
	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.find_jkgc_title_bar);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		titleBar.onClickBack(this);
		tvTitle.setText(getString(R.string.find_1));
		tvBack.setText(getString(R.string.main_tab_tv_find));
	}

	private void tabPage() {
		// ViewPager的adapter
		pager = (ViewPager) findViewById(R.id.pager);
		pager.setOffscreenPageLimit(4);
		// 显示tab标题
		FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(
				getSupportFragmentManager());

		pager.setAdapter(adapter);

		// 实例化TabPageIndicator然后设置ViewPager与之关联
		TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
		indicator.setViewPager(pager);

		// 如果我们要对ViewPager设置监听，用indicator设置就行了
		indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				// Toast.makeText(getApplicationContext(), TITLE[arg0],
				// Toast.LENGTH_SHORT).show();
				Stattistics(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

	}

	/**
	 * ViewPager适配器
	 * 
	 * @author len
	 * 
	 */
	class TabPageIndicatorAdapter extends FragmentPagerAdapter {
		public TabPageIndicatorAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// 新建一个Fragment来展示ViewPager item的内容，并传递参数
			Fragment fragment = new JkgcPlazeFragment();
			Bundle args = new Bundle();
			// args.putString("arg", TITLE[position]);
			// List<JkgcData> data = getJkgcData(TITLE_KEY[position]);
			args.putString("target", TITLE_KEY[position]);
			args.putString("title", TITLE[position]);
			// if (data != null) {
			// args.putSerializable("titles", (java.io.Serializable) data);
			// }
			fragment.setArguments(args);
			return fragment;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLE[position % TITLE.length];
		}

		@Override
		public int getCount() {
			return TITLE.length;
		}
	}
	
	/**
	 * 友盟统计
	 */
	public void Stattistics(int position){
		Map<String, String> click = new HashMap<String, String>();
		click.put("plate",TITLE[position]);
		MobclickAgent.onEvent(this, "B00111", click);
	}

}
